// components/pay/pay.js
import pay from '../../model/pay'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    order: {
      type: Object,
      value: {}
    },
    show: {
      type: Boolean,
      value: 'false'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    radioItems: [{
      name: '微信支付',
      value: '1',
      checked: true
    }],
    type: 'success',
    title: '操作成功',
    descr: 'Result 结果页，在页面中组织图标、文字等内容，向用户反馈操作结果'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 支付
     * @param {*} e 
     */

    //支付
    pay() {
      let that = this
      let reqData = {
        id: this.data.order.id
      }
      pay.prePay(reqData, (res => {
        if (res.code === 200) {
          let type = 1
          const reqPay = res.data
          wx.requestPayment({
            "timeStamp": reqPay.timeStamp,
            "nonceStr": reqPay.nonceStr,
            "package": reqPay.package,
            "signType": "RSA",
            "paySign": reqPay.paySign,
            "success": function (res) {
              wx.showToast({
                title: '支付成功',
              })
              type = 1
            },
            "fail": function (res) {
              console.log('支付失败:', res)
              type = 2
            },
            "complete": function (res) {
              //关闭支付窗口
              that.setData({
                paySwitch: false
              })
              wx.navigateTo({
                url: '/pages/result/result?type=' + type,
              })
            }
          })
        } else if (res.code == 204) {
          wx.navigateTo({
            url: '/pages/result/result?type=' + 1,
          })
        } else {
          wx.showToast({
            title: res.msg,
          })
        }
        this.setData({
          show: false
        })
      }))

    },
    change(e) {
      this.setData({
        method: 1
      })
    },
    //调用此方法显示底部弹出层
    showPopup(type) {
      this.setData({
        show: true
      })
    },
    closePopup(type) {
      this.setData({
        show: false
      })
    }

  }
})